package cn.caplike.demo.spring.bean.lifecycle.bean.post.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * BeanPostProcessor 的使用<br>
 * Spring provides the BeanPostProcessor interface that gives you the means to tap into the Spring context life cycle and interact with beans as they are processed.<br>
 * At runtime, Spring will inject the new bean instance and the name of the bean to both the methods.
 *
 * @author LiKe
 * @version 1.0.0
 * @date 2020-03-31 16:30
 */
@Component
public class BookPostProcessor implements BeanPostProcessor {

    /**
     * Spring calls the postProcessBeforeInitialization() method after calling the methods of the aware interfaces.
     * 当 *Aware 接口的方法被调用后, Spring 会调用这个方法
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Post Process Before Initialization method is called: Bean Name " + beanName);
        return bean;
    }

    /**
     * Spring calls the postProcessAfterInitialization() method after any bean initialization callbacks,
     * such as InitializingBean’s afterPropertiesSet or a custom init-method.
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Post Process After Initialization method is called: Bean Name " + beanName);
        return null;
    }
}
